package com.jinduplan.planmanager.ui.activity.msg

import android.content.Intent
import android.util.Log
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.cxz.kotlin.baselibs.adapter.CustomRecyclerAdapter
import com.cxz.kotlin.baselibs.adapter.ViewHolder
import com.cxz.kotlin.baselibs.base.BaseMvpActivity
import com.cxz.kotlin.baselibs.utils.GsonHelper
import com.jinduplan.planmanager.R
import com.jinduplan.planmanager.bean.DataProjectBean
import com.jinduplan.planmanager.bean.MessageBean
import com.jinduplan.planmanager.bean.MsgProjctBean
import com.jinduplan.planmanager.mvp.contract.MessageContract
import com.jinduplan.planmanager.mvp.presenter.MessagePresenter
import com.jinduplan.planmanager.ui.activity.project.ProjectDetailActivity
import com.jinduplan.planmanager.ui.activity.project.ProjectUploadDetailActivity
import com.jinduplan.planmanager.utils.GlideUtil
import com.jinduplan.planmanager.utils.ResUtil
import com.jinduplan.planmanager.widgets.CircleImageView
import com.luck.indicator.EasyIndicator
import kotlinx.android.synthetic.main.activity_message.*
import kotlinx.android.synthetic.main.activity_project.*
import kotlinx.android.synthetic.main.activity_project.back
import kotlinx.android.synthetic.main.activity_project.recyclerView
import kotlinx.android.synthetic.main.activity_project.swipeRefreshLayout


class MessageActivity : BaseMvpActivity<MessageContract.View, MessageContract.Presenter>(),
    MessageContract.View, EasyIndicator.onTabClickListener {


    override fun createPresenter(): MessageContract.Presenter = MessagePresenter()

    private var messageBeanList: ArrayList<MessageBean.DataBean>? = null
    private var allList: ArrayList<MessageBean.DataBean>? = null
    private var readBeanList: ArrayList<MessageBean.DataBean>? = null
    private var unReadBeanList: ArrayList<MessageBean.DataBean>? = null
    private var mPosition: Int = 0


    override fun start() {
        messageBeanList = ArrayList()
        allList = ArrayList()
        readBeanList = ArrayList()
        unReadBeanList = ArrayList()
        recyclerView.adapter = object : CustomRecyclerAdapter<MessageBean.DataBean>(
            this, R.layout.item_message, messageBeanList
        ) {
            override fun convert(
                holder: ViewHolder?,
                t: MessageBean.DataBean?,
                position: Int
            ) {
                holder?.setText(R.id.tv_title, t?.title)
                holder?.setText(R.id.tv_content, t?.content)
                holder?.setText(R.id.tv_date, t?.created_at)
                GlideUtil.load("", holder?.getView<CircleImageView>(R.id.iv_head))
                var view = holder?.getView<View>(R.id.view_unread)
                if (t?.is_read == 0) {
                    view?.visibility = View.VISIBLE
                } else {
                    view?.visibility = View.GONE
                }
                holder?.convertView?.setOnClickListener {
//                    var intent = Intent(applicationContext, MessageDetailActivity::class.java)
//                    intent.putExtra("msg", t)
//                    startActivity(intent)

                    var projectBean = GsonHelper.parse(t?.data, MsgProjctBean::class.java)
                    mPresenter?.getProjectList(
                        1,
                        500,
                        projectBean.project_id.toInt(),
                        projectBean.audit_id,
                        t!!
                    )
                }
            }
        }
        recyclerView.layoutManager = LinearLayoutManager(this)

        swipeRefreshLayout.setOnRefreshListener {
            mPresenter?.getMsgList(1, 500)
        }


    }

    override fun onResume() {
        super.onResume()
        mPresenter?.getMsgList(1, 500)
    }

    override fun initView() {
        super.initView()
        setStatusBarColor(ResUtil.getC(R.color.white))
        back.setOnClickListener { finish() }
        easyIndicator.setTabTitles(arrayOf("全部", "已读", "未读"))
        easyIndicator.setOnTabClickListener(this)
    }


    override fun attachLayoutRes(): Int {
        return R.layout.activity_message
    }


    override fun setMsgListData(data: MessageBean) {
        swipeRefreshLayout.isRefreshing = false
        messageBeanList?.clear()
        unReadBeanList?.clear()
        readBeanList?.clear()
        allList?.clear()
        messageBeanList?.addAll(data.data)
        allList?.addAll(data.data)
        if (messageBeanList!!.size > 0) {
            for (i in 0 until messageBeanList!!.size) {
                if (messageBeanList!![i].is_read == 0) {
                    unReadBeanList!!.add(messageBeanList!![i])
                } else {
                    readBeanList!!.add(messageBeanList!![i])
                }
            }
        }
        changeData(mPosition)
    }


    override fun readMsgSuccess() {
    }

    override fun setProjectListData(projectData: DataProjectBean, msg: MessageBean.DataBean) {
        if (projectData.data.size > 0) {
            var status = projectData.data[0].audit.status
            //审核状态 0 待审核 1 审核通过 2 审核驳回
            if (status == 2) {
                val intent = Intent(applicationContext, ProjectUploadDetailActivity::class.java)
                intent.putExtra("project_id", projectData.data[0].project_id)
                startActivity(intent)
            } else {
                val intent = Intent(applicationContext, ProjectDetailActivity::class.java)
                if (projectData.data[0].project.project_name.isEmpty()) {
                    intent.putExtra("project_name", "消息详情")
                } else {
                    intent.putExtra("project_name", projectData.data[0].project.project_name)
                }
                intent.putExtra("msg", msg)
                intent.putExtra("project_id", projectData.data[0].project_id)
                startActivity(intent)
            }
        }
    }

    override fun onTabClick(text: String?, position: Int) {
        Log.e("onTabClick", text + " position:" + position)
        mPosition = position
        changeData(position)
    }

    private fun changeData(position: Int) {
        when (position) {
            0 -> {
                messageBeanList?.clear()
                messageBeanList?.addAll(allList!!)
            }
            1 -> {
                messageBeanList?.clear()
                messageBeanList?.addAll(readBeanList!!)
            }
            2 -> {
                messageBeanList?.clear()
                messageBeanList?.addAll(unReadBeanList!!)
            }
        }
        recyclerView.adapter?.notifyDataSetChanged()
    }


}